1 /**
2  * License:
3  *              Copyright Devisualization (Richard Andrew Cattermole) 2014 - 2017.
4  *     Distributed under the Boost Software License, Version 1.0.
5  *        (See accompanying file LICENSE_1_0.txt or copy at
6  *              http://www.boost.org/LICENSE_1_0.txt)
7  */
8 module devisualization.util.photoshop_aco.loader;
9 import devisualization.util.photoshop_aco.defs;
10 
11 void parseACO(ref ACOPalette _, inout ubyte[] data) {
12 	uint offset = 0;
13 
14 	if (data.getValue(offset) == 1) {
15 		offset = readData(_, data);
16 	}
17 
18 	if (data.getValue(offset) == 2) {
19 		readData(_, data, offset);
20 	}
21 }
22 
23 ushort getValue(inout ubyte[] data, uint index) {
24 	import std.bitmanip : bigEndianToNative;
25 
26 	uint start = index * 2;
27 	ubyte[2] values = data[start .. start + 2];
28 
29 	return bigEndianToNative!ushort(values);
30 }
31 
32 uint getValueInt(inout ubyte[] data, uint index) {
33 	import std.bitmanip : bigEndianToNative;
34 	
35 	uint start = index * 2;
36 	ubyte[4] values = data[start .. start + 4];
37 	
38 	return bigEndianToNative!uint(values);
39 }
40 
41 uint readData(ref ACOPalette _, inout ubyte[] data, uint j = 0) {
42 	import devisualization.image.color;
43 	ushort ver = data.getValue(j);
44 
45 	ushort count = data.getValue(j + 1);
46 	j += 2;
47 
48 	for(uint i = 0; i < count; i++) {
49 		if (ver == 2) {
50 			j += 5;
51 
52 			uint length = data.getValueInt(j);
53 			j += 2;
54 
55 			dstring text;
56 			foreach(k; j .. j + length - 1) {
57 				text ~= cast(dchar)data.getValue(k);
58 			}
59 
60 			_.names ~= text;
61 
62 			j += length;
63 		} else if (ver == 1) {
64 			ushort color_space = data.getValue(j);
65 			ushort w = data.getValue(j + 1);
66 			ushort x = data.getValue(j + 2);
67 			ushort y = data.getValue(j + 3);
68 			ushort z = data.getValue(j + 4);
69 			
70 			if (color_space == 0) {
71 				_.colors ~= new Color_RGBA(w, x, y, ushort.max);
72 			} else {
73 				throw new Exception("Unknown color space");
74 			}
75 			
76 			j += 5;
77 		}
78 	}
79 
80 	return j;
81 }